package com.h3c.nem.utils;

import com.h3c.nem.async.AsyncSendMessage;
import com.h3c.nem.entity.SdAllWorker;
import com.h3c.nem.entity.SdEmailTemplate;
import com.h3c.nem.entity.SdNoticeRecord;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.service.ISdAllWorkerService;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ISdEmailTemplateService;
import com.h3c.nem.service.ISdNoticeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;

@Slf4j
@Component
public class SendMessageUtil {

    @Resource
    private ISdEmailTemplateService emailTemplateService;
    @Resource
    private ISdNoticeRecordService iSdNoticeRecordService;
    @Resource
    private AsyncSendMessage asyncSendMessage;
    @Resource
    private ISdDeptService deptService;
    @Resource
    private ISdAllWorkerService allWorkerService;

    public void sendEmailToOfferWorker(TbOfferWorker offerWorker, String emailTemplateCode, Map<String, String> contentMap, Boolean needSms) {
        log.info("员工数据：{}", offerWorker);
        log.info("邮件内容：code={}, contentMap={}, needSms={}", emailTemplateCode, contentMap, needSms);
        //邮件内容
        SdEmailTemplate template = emailTemplateService.getById(emailTemplateCode);
        String templateTitle = template.getEmailTemplateTitle();
        String templateContent = getContent(template, contentMap);

        //员工数据
        String workerId = offerWorker.getWorkerId();
        String workerName = offerWorker.getWorkerName();

        //短信
        if (needSms) {
            SdNoticeRecord smsRecord = iSdNoticeRecordService.createSmsNoticeRecord(workerId, workerName,
                    offerWorker.getPhoneNo(), templateTitle, templateContent);
            asyncSendMessage.sendMessage(Collections.singletonList(smsRecord));
        }
        //邮件
        SdNoticeRecord emailRecord = iSdNoticeRecordService.createEmailNoticeRecord("-1", workerId, workerName,
                offerWorker.getEmail(), templateTitle, templateContent, "");
        asyncSendMessage.sendMessage(Collections.singletonList(emailRecord));
    }

    public void sendEmailToSSCAndBp(String emailTemplateCode, Map<String, String> contentMap, String deptNameChain,
                                    String workCity) {
        //邮件内容
        SdEmailTemplate template = emailTemplateService.getById(emailTemplateCode);
        String templateTitle = template.getEmailTemplateTitle();
        String templateContent = getContent(template, contentMap);

        Map<String, String> map = deptService.getBpAndContactorByDeptNameChain(deptNameChain, workCity);

        SdNoticeRecord emailNoticeRecord = iSdNoticeRecordService.createEmailNoticeRecord("-1", map.get("ids"),
                map.get("names"), map.get("emails"), templateTitle, templateContent, "");
        asyncSendMessage.sendMessage(Collections.singletonList(emailNoticeRecord));
    }

    public String getContent(SdEmailTemplate template, Map<String, String> contentMap) {
        String templateContent = template.getEmailTemplateContent();
        for (Map.Entry<String, String> entry : contentMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            templateContent = templateContent.replace(key, value);
        }
        return templateContent;
    }

    public String getOfferWorkerBp(String deptNamChain) {
        String bpUserId = deptService.getBpByDeptNameChain(deptNamChain);
        String workerId = bpUserId.split(",")[0];
        SdAllWorker bp = allWorkerService.getById(workerId);
        if (bp == null) {
            return "";
        }
        return bp.getWorkerName() + ":" + bp.getPhoneNo();
    }
}
